import {brandList, priceDetail} from "../services/api";

export default {

  namespace: 'cars',

  state: {
    brands: {},
    activeBrand: null,
    activeSeries: {
      children: []
    },
    // 价格查询表单
    formData: {},
    priceDetail: {}
  },


  effects: {
    //品牌 +车系 + 车型
    * fetch_brands({payload, cb}, {call, put}) {
      const response = yield call(brandList);
      console.log('品牌 +车系 + 车型', response);
      yield put({type: 'saveBrands', payload: response});
      cb && cb();
    },

    * set_brand({payload}, {call, put}) {
      yield put({type: 'setBrand', payload: payload});
    },

    * set_series({payload}, {call, put}) {
      yield put({type: 'setSeries', payload: payload});
    },

    * save_formData({payload}, {call, put}) {
      yield put({type: 'setFormData', payload: payload});
    },

// 询价
    * search_price({payload}, {call, put}) {
      const response = yield call(priceDetail, payload);
      console.log('response',response);
      yield put({type:'setPriceDetailData', payload: response.data});
    }
  },

  reducers: {
    saveBrands(state, {payload}) {
      return {...state, brands: payload};
    },

    setBrand(state, {payload}) {
      return {...state, activeBrand: payload};
    },

    setSeries(state, {payload}) {
      return {...state, activeSeries: payload};
    },

    setFormData(state, {payload}) {
      var oldFormData = state.formData;
      var formData = {
        ...oldFormData,
        ...payload
      }
      return {...state, formData};
    },
    // 保存询价结果
    setPriceDetailData(state, {payload}) {

      return {...state, priceDetail:payload};
    }
  }
};
